//
//  ViewController.m
//  SSOTest
//
//  Created by lianglibao on 2018/7/28.
//  Copyright © 2018年 梁立保. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "UIView+VLAdditions.h"
#import "QImageView.h"
#import "Question.h"
#import "History.h"
#import "Topic.h"


@interface ViewController ()
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) NSMutableArray <Question *> *questionModelArr;
@property (nonatomic, strong) NSMutableDictionary *allTopicDic;

@property (nonatomic, strong) NSMutableArray *history_topic;

@property (nonatomic, weak) UIButton *ageButtonOne;
@property (nonatomic, weak) UIButton *ageButtonTwo;
@property (nonatomic, weak) UIButton *ageButtonThree;
@property (nonatomic, weak) UILabel *showTopicLabel;

@property (nonatomic, weak) QImageView *answerOne;
@property (nonatomic, weak) QImageView *answerTwo;
@property (nonatomic, weak) QImageView *answerThree;

@property (nonatomic, weak) UIButton *resetBtn;

@property (nonatomic, strong) Topic *curTopic;
@property (nonatomic, assign) NSInteger normalLevel;
@property (nonatomic, strong) Topic *gradeTopic;
@end

@implementation ViewController

- (NSMutableArray<Question *> *)questionModelArr {
    if (!_questionModelArr) {
        _questionModelArr = [NSMutableArray array];
    }
    return _questionModelArr;
    
}

- (NSMutableDictionary *)allTopicDic {
    if (!_allTopicDic) {
        _allTopicDic = [NSMutableDictionary dictionaryWithCapacity:13];
    }
    return _allTopicDic;
}

- (NSMutableArray *)history_topic {
    if (!_history_topic) {
        _history_topic = [NSMutableArray array];
    }
    return _history_topic;
}

- (dispatch_queue_t)queue {
    if (!_queue) {
        _queue = dispatch_get_global_queue(0, 0);
    }
    return _queue;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self p_loadData];
    
    [self seupSubViews];

}

- (void)seupSubViews {
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    
    [self.view addSubview:({
        UIButton *ageButtonOne = [[UIButton alloc] initWithFrame:CGRectMake(30, 64, 60, 50)];
        [ageButtonOne setBackgroundColor:[UIColor grayColor]];
        [ageButtonOne setTitle:@"0-3岁" forState:UIControlStateNormal];
        [ageButtonOne addTarget:self action:@selector(pickAgeAction:) forControlEvents:UIControlEventTouchUpInside];
        _ageButtonOne = ageButtonOne;
        ageButtonOne;
    })];
    
    [self.view addSubview:({
        UIButton *ageButtonTwo = [[UIButton alloc] initWithFrame:CGRectMake(self.ageButtonOne.right + 30, 64, 60, 50)];
        [ageButtonTwo setBackgroundColor:[UIColor grayColor]];
        [ageButtonTwo setTitle:@"4-6岁" forState:UIControlStateNormal];
        [ageButtonTwo addTarget:self action:@selector(pickAgeAction:) forControlEvents:UIControlEventTouchUpInside];
        _ageButtonTwo = ageButtonTwo;
        ageButtonTwo;
    })];
    
    [self.view addSubview:({
        UIButton *ageButtonThree = [[UIButton alloc] initWithFrame:CGRectMake(self.ageButtonTwo.right + 30, 64, 60, 50)];
        [ageButtonThree setBackgroundColor:[UIColor grayColor]];
        [ageButtonThree setTitle:@"7-12岁" forState:UIControlStateNormal];
        [ageButtonThree addTarget:self action:@selector(pickAgeAction:) forControlEvents:UIControlEventTouchUpInside];
        _ageButtonThree = ageButtonThree;
        ageButtonThree;
    })];
    
    [self.view addSubview:({
        UILabel *showTopicLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.ageButtonOne.bottom + 30, screenW, 100)];
        showTopicLabel.font = [UIFont systemFontOfSize:34];
        showTopicLabel.textColor = [UIColor blackColor];
        showTopicLabel.textAlignment = NSTextAlignmentCenter;
        _showTopicLabel = showTopicLabel;
        showTopicLabel;
    })];
    
    
    CGFloat imageWH = (screenW - 15 *2)/3.0;
    [self.view addSubview:({
        QImageView *answerOne = [[QImageView alloc] initWithFrame:CGRectMake(0, self.showTopicLabel.bottom + 50, imageWH, imageWH)];
        answerOne.contentMode = UIViewContentModeScaleAspectFit;
        answerOne.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickAnswerAction:)];
        [answerOne addGestureRecognizer:tap];
        _answerOne = answerOne;
        answerOne;
    })];
    
    [self.view addSubview:({
        QImageView *answerTwo = [[QImageView alloc] initWithFrame:CGRectMake(self.answerOne.right + 15, self.showTopicLabel.bottom + 50, imageWH, imageWH)];
        answerTwo.contentMode = UIViewContentModeScaleAspectFit;
        answerTwo.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickAnswerAction:)];
        [answerTwo addGestureRecognizer:tap];
        _answerTwo = answerTwo;
        answerTwo;
    })];
    
    [self.view addSubview:({
        QImageView *answerThree = [[QImageView alloc] initWithFrame:CGRectMake(self.answerTwo.right + 15, self.showTopicLabel.bottom + 50, imageWH, imageWH)];
        answerThree.contentMode = UIViewContentModeScaleAspectFit;
        answerThree.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickAnswerAction:)];
        [answerThree addGestureRecognizer:tap];
        _answerThree = answerThree;
        answerThree;
    })];
    
    [self.view addSubview:({
        UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(0 , self.answerThree.bottom + 50, 100, 30)];
        resetBtn.centerX = screenW / 2.0;
        resetBtn.titleLabel.font = [UIFont systemFontOfSize:24];
        [resetBtn setTitle:@"重新测试" forState:UIControlStateNormal];
        [resetBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [resetBtn setBackgroundColor:[UIColor lightGrayColor]];
        [resetBtn addTarget:self action:@selector(restAction) forControlEvents:UIControlEventTouchUpInside];
        resetBtn;
    })];
    
}

- (void)restAction {
    if (!self.history_topic.count) {
        return;
    }
    self.gradeTopic = nil;
    
    [self p_loadData];
    
    self.ageButtonOne.enabled = YES;
    self.ageButtonTwo.enabled = YES;
    self.ageButtonThree.enabled = YES;
    self.ageButtonOne.hidden = NO;
    self.ageButtonTwo.hidden = NO;
    self.ageButtonThree.hidden = NO;
    
    self.answerOne.image = nil;
    self.answerTwo.image = nil;
    self.answerThree.image = nil;
    self.answerOne.hidden = NO;
    self.answerTwo.hidden = NO;
    self.answerThree.hidden = NO;
    
    self.answerOne.flag = NO;
    self.answerTwo.flag = NO;
    self.answerThree.flag = NO;
    
    [self.history_topic removeAllObjects];
}

- (void)pickAgeAction:(UIButton *)ageBtn {
    if (ageBtn == self.ageButtonOne) {
        self.ageButtonTwo.hidden = YES;
        self.ageButtonThree.hidden = YES;
        ageBtn.enabled = NO;
        // 出题
        Question *question = self.allTopicDic[@(0)];
        Topic *topic =  question.topics.firstObject;
        self.curTopic = topic;
        self.normalLevel = 0;
        [question.topics removeObject:topic];
        
    } else if (ageBtn == self.ageButtonTwo) {
        self.ageButtonOne.hidden = YES;
        self.ageButtonThree.hidden = YES;
        ageBtn.enabled = NO;
        // 出题
        Question *question = self.allTopicDic[@(1)];
        Topic *topic =  question.topics.firstObject;
        self.curTopic = topic;
        self.normalLevel = 1;
        [question.topics removeObject:topic];

    } else {
        self.ageButtonOne.hidden = YES;
        self.ageButtonTwo.hidden = YES;
        ageBtn.enabled = NO;
        // 出题
        Question *question = self.allTopicDic[@(2)];
        Topic *topic =  question.topics.firstObject;
        self.curTopic = topic;
        self.normalLevel = 2;
        [question.topics removeObject:topic];
    }
}

- (void)p_handleTopicImage:(Topic *)topic {
    [self.answerOne sd_setImageWithURL:[NSURL URLWithString:((Quiz *)topic.quiz[0]).pic_path]
                      placeholderImage:nil
                               options:SDWebImageLowPriority | SDWebImageContinueInBackground | SDWebImageRetryFailed];
    self.answerOne.flag = ((Quiz *)topic.quiz[0]).flag;
    
    [self.answerTwo sd_setImageWithURL:[NSURL URLWithString:((Quiz *)topic.quiz[1]).pic_path]
                      placeholderImage:nil
                               options:SDWebImageLowPriority | SDWebImageContinueInBackground | SDWebImageRetryFailed];
    self.answerTwo.flag = ((Quiz *)topic.quiz[1]).flag;
    
    [self.answerThree sd_setImageWithURL:[NSURL URLWithString:((Quiz *)topic.quiz[2]).pic_path]
                      placeholderImage:nil
                               options:SDWebImageLowPriority | SDWebImageContinueInBackground | SDWebImageRetryFailed];
    self.answerThree.flag = ((Quiz *)topic.quiz[2]).flag;

}

- (void)pickAnswerAction:(UITapGestureRecognizer *)tap {
    if (tap.view == self.answerOne) {
        // 判断答案-->下一个/弹窗
        [self p_handleTpoic:self.answerOne.flag];
        
    } else if (tap.view == self.answerTwo) {
        
        // 判断答案-->下一个/弹窗
        [self p_handleTpoic:self.answerTwo.flag];
    } else {
        
        // 判断答案-->下一个/弹窗
        [self p_handleTpoic:self.answerThree.flag];
    }
}

- (void)p_handleTpoic:(BOOL)flag {
    if (self.gradeTopic) {
        [self p_saveHistoryTopic:self.gradeTopic flag:flag];
        [self gradeCompleteWithTopic:self.gradeTopic];
        return;
    }

    Topic *lastTopic =  ((History *)self.history_topic.lastObject).topic;
    if (self.normalLevel == 0) {
        [self p_grade0WithLastTopic:lastTopic flag:flag];
    } else if (self.normalLevel == 1) {
        [self p_grade1WithLastTopic:lastTopic flag:flag];
    } else if (self.normalLevel == 2) {
        [self p_grade2WithLastTopic:lastTopic flag:flag];
    } else if (self.normalLevel == 3) {
        [self gradeCompleteWithTopic:@"D"];
    }
}

- (void)setCurTopic:(Topic *)curTopic {
    _curTopic = curTopic;
    
    [self p_handleTopicImage:curTopic];
    
    self.showTopicLabel.text = curTopic.title_json.title;
}

- (void)p_saveHistoryTopic:(Topic *)lastTopic flag:(BOOL)flag {
    History *history = [History new];
    history.bigLevel = lastTopic.bigLevel;
    history.level = lastTopic.source_id;
    history.topic = lastTopic;
    history.isTrue = flag;
    [self.history_topic addObject:history];
}

- (void)showAlertWithTopic:(Topic *)topic flag:(BOOL)flag {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"是否升级题型?" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    __weak typeof(self) weakSelf = self;
    UIAlertAction *gradeAction = [UIAlertAction actionWithTitle:@"直接定级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 定级
        [weakSelf gradeCompleteWithTopic:topic];
    }];
    
    UIAlertAction *upGradeAction = [UIAlertAction actionWithTitle:@"升级题型" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        Question *question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];
        
        __block Topic *greadeTopic;
        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.source_id == (topic.source_id + 1)) {
                greadeTopic = obj;
                *stop = YES;
            }
        }];
        [question.topics removeObject:greadeTopic];
        weakSelf.curTopic = greadeTopic;
        [weakSelf p_saveHistoryTopic:topic flag:flag];
    }];
    
    [alertVC addAction:gradeAction];
    [alertVC addAction:upGradeAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)gradeCompleteWithTopic:(id)gradeTopic {
    NSString *gradeStr = @"aa";
    if ([gradeTopic isKindOfClass:Topic.class]) {
        switch (((Topic *)gradeTopic).source_id) {
            case 3:
                gradeStr = @"aa";
                break;
            case 4:
                gradeStr = @"A";
                break;
            case 5:
                gradeStr = @"B";
                break;
            case 6:
                gradeStr = @"C";
                break;
            case 7:
                gradeStr = @"D";
                break;
            case 8:
                gradeStr = @"E";
                break;
            case 9:
                gradeStr = @"F";
                break;
            case 10:
                gradeStr = @"G";
                break;
            case 11:
                gradeStr = @"H";
                break;
            case 12:
                gradeStr = @"I";
                break;
            default:
                gradeStr = @"J";
                break;
        }
        
    } else {
        gradeStr = (NSString *)gradeTopic;
    }
    
    NSString *basicGrade = @"0-3岁";
    switch (self.normalLevel) {
        case 0:
            basicGrade = @"0-3岁 aa";
            break;
        case 1:
            basicGrade = @"4-6岁 B";
            break;
        case 2:
            basicGrade = @"7-12岁 D";
            break;
        default:
            break;
    }
    
    self.showTopicLabel.text = [NSString stringWithFormat:@"初始级别:%@-->最终定级:%@", basicGrade, gradeStr];
    self.showTopicLabel.font = [UIFont systemFontOfSize:20];
    self.answerOne.hidden = YES;
    self.answerTwo.hidden = YES;
    self.answerThree.hidden = YES;
}

- (void)p_loadData {
    [self.questionModelArr removeAllObjects];
    [self.allTopicDic removeAllObjects];
    
    dispatch_async(self.queue, ^{
        NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"topic_list.json" ofType:nil];
        NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
        if (jsonData) {
            NSArray *jsonArr = [NSJSONSerialization JSONObjectWithData:jsonData options
                                                                      :NSJSONReadingMutableContainers
                                                                 error:nil];
            
            [jsonArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                Question *question = [Question yy_modelWithDictionary:obj];
                if (question) {
                    [self.questionModelArr addObject:question];
                }
            }];
        }
        NSLog(@"%@", self.questionModelArr);
        
        if (self.questionModelArr.count) {
//            NSMutableArray *tmpLevelArr = [NSMutableArray arrayWithCapacity:3];
//            self.questionModelArr = [self getSameArrayList:self.questionModelArr resArray:tmpLevelArr];
//            NSLog(@"%@", self.questionModelArr);
            [self.questionModelArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [self.allTopicDic setObject:obj forKey:@(idx)];
            }];
            
            NSLog(@"%@", self.allTopicDic);
        }
    });
}

- (NSMutableArray *)getSameArrayList:(NSArray *)arrayList resArray:(NSMutableArray *)_resArray
{
    NSMutableArray *diffArray = [NSMutableArray array];
    NSMutableArray *sameArray = [NSMutableArray array];
    [sameArray addObject:arrayList[0]];
    for (int i = 1; i < arrayList.count; i ++) {
        Topic *topic = sameArray.firstObject;
        Topic *tmpTopic = arrayList[i];
        if (topic.bigLevel == tmpTopic.bigLevel) {
            [sameArray addObject:tmpTopic];
        } else {
            [diffArray addObject:tmpTopic];
        }
    }
    [_resArray addObject:sameArray];
    if (diffArray.count != 0) {
        [self getSameArrayList:diffArray resArray:_resArray];
    }
    return _resArray;
}

// MARK: - Grade Test Nucleus Codes
- (void)p_grade0WithLastTopic:(Topic *)lastTopic flag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.normalLevel)];
    Topic *curLevelLast = question.topics.lastObject;
    if (flag) {
        
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            
            if ((historyCount == 1) && !history.isTrue) {
                // aa ❌ aa✅ 直接定级aa
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 1 && history.isTrue) {
                // aa✅ A✅ ==>抽题A 不同题型
                __block Topic *topic;
                [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.source_id == self.curTopic.source_id) {
                        topic = obj;
                        *stop = YES;
                    }
                }];
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                return;
            } else if ((historyCount == 2) &&
                       history.isTrue &&
                       (curLevelLast.source_id = self.curTopic.source_id)) {
                // aa✅ A✅ A✅ ==> 弹出升级
                // 弹出升级提示
                [self showAlertWithTopic:self.curTopic flag:flag];
                return;
            } else if ((historyCount == 3) && history.isTrue) {
                // aa✅ A✅ A✅ B✅ ==> 抽C
                question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                __block Topic *topic;
                [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.source_id == (self.curTopic.source_id + 1)) {
                        topic = obj;
                        *stop = YES;
                    } else {
                        if (idx == (question.topics.count - 1)) {
                            topic = obj;
                            *stop = YES;
                        }
                    }
                }];
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                return;
            } else if ((historyCount == 3) && !history.isTrue) {
                // aa✅ A✅ A❌ aa✅ ==> 定级C
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 4) {
                // aa✅ A✅ A✅ B✅ C✅ ==> 抽C
                [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                return;
            } else if (historyCount == 5) {
                // aa✅ A✅ A✅ B✅ C❌ B✅ ==> 定级B
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 6) {
                // aa✅ A✅ A✅ B✅ C✅ C❌ B✅ ==> 定级B
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            }
        } else {
            // aa✅ ==> 抽A
            __block Topic *topic;
            [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.source_id == (self.curTopic.source_id + 1)) {
                    topic = obj;
                    *stop = YES;
                }
            }];
            [question.topics removeObject:topic];
            [self p_saveHistoryTopic:self.curTopic flag:flag];
            self.curTopic = topic;
            return;
        }
    } else {
        
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            if ((historyCount == 1) && !history.isTrue) {
                // aa❌ aa❌ ==> 定级aa
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if ((historyCount == 1) && history.isTrue) {
                // aa✅ A❌ ==> aa (不管对错直接定级.)
                __block Topic *topic;
                [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.source_id == lastTopic.source_id) {
                        // aa
                        topic = obj;
                        *stop = YES;
                    }
                }];
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                self.gradeTopic = topic;
                return;
            } else if ((historyCount == 2) && (lastTopic.source_id > self.curTopic.source_id)) {
                // aa✅ A❌ aa❌ 定级aa
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if ((historyCount == 2) && (lastTopic.source_id = self.curTopic.source_id)) {
                // aa✅ A✅ A❌ 抽aa (不管对错直接定级.)
                Topic *topic = question.topics.firstObject;
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                self.gradeTopic = topic;
                return;
            } else if (historyCount == 3) {
                // aa✅ A✅ A✅ B❌ ==> 定级A
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:lastTopic];
                return;
            } else if (historyCount == 4) {
                // aa✅ A✅ A✅ B✅ C❌ ==> 抽B
                __block Topic *topic;
                question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if (obj.source_id == lastTopic.source_id) {
                        topic = obj;
                        *stop = YES;
                    }
                }];
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                return;
            } else if (historyCount == 5) {
                if (self.curTopic.source_id == lastTopic.source_id) {
                    // aa✅ A✅ A✅ B✅ C✅ C❌ ==> 抽B
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                } else {
                    // aa✅ A✅ A✅ B✅ C❌ B❌ ==> 定级A
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    Topic *gradeTopic = question.topics.lastObject;
                    [self gradeCompleteWithTopic:gradeTopic];
                }
            } else if (historyCount == 6) {
                // aa✅ A✅ A✅ B✅ C✅ C❌ B❌==> 定级A
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                Topic *gradeTopic = question.topics.lastObject;
                [self gradeCompleteWithTopic:gradeTopic];
            }
            return;
        } else {
            // aa❌ 抽aa (不管对错直接定级.)
            __block Topic *topic;
            [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.source_id == self.curTopic.source_id) {
                    topic = obj;
                    *stop = YES;
                }
            }];
            [question.topics removeObject:topic];
            [self p_saveHistoryTopic:self.curTopic flag:flag];
            self.curTopic = topic;
            self.gradeTopic = topic;
            return;
        }
    }
}

- (void)p_grade1WithLastTopic:(Topic *)lastTopic flag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.normalLevel)];
    if (flag) {
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            if (historyCount == 1) {
                if (history.isTrue) {
                    // B✅ C✅==> 抽C
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    // B❌ A✅==> 抽A
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 2) {
                // B❌ A✅ A✅ ==> 定级A
                if (self.curTopic.bigLevel < self.normalLevel) {
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:lastTopic];
                    return;
                } else {
                    // B✅ C✅ C✅ ==> 弹出弹窗.
                    if (self.curTopic.source_id == lastTopic.source_id) {
                        [self showAlertWithTopic:self.curTopic flag:flag];
                        return;
                    } else {
                        // B✅ C❌ B✅
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                        return;
                    }
                }
            } else if (historyCount == 3) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ ==> 抽E
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // B✅ C❌ B❌ A✅ ==> 抽A
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // B✅ C✅ C❌ B✅ ==> 定级B
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                    }
                    return;
                }
            } else if (historyCount == 4) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    if (self.curTopic.source_id < lastTopic.source_id) {
                        // B✅ C✅ C❌ B❌ A✅ ==> 抽A
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // B✅ C❌ B❌ A✅ A✅ ==> 定级A
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                    }
                } else {
                    // B✅ C✅ C✅ D✅ E✅ ==> 抽F
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    
                }
                return;
            } else if (historyCount == 5) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ ==> 抽G
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E❌ D✅ ==> 定级D
                    // B✅ C✅ C❌ B❌ A✅ A✅ ==> 定级A
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 6) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ ==> 抽H
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F❌ E✅ ==> 定级E
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 7) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ ==> 抽I
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G❌ F✅ ==> 定级F
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 8) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ ==> 抽J
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ G✅ ==> 定级G
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 9) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ ==> 抽J
                    [self p_fetchHighLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G❌ F❌ E❌ D✅ ==> 定级D
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ G❌ H✅ ==> 定级H
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ G❌ F✅ ==> 定级F
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 10) {
                if (self.curTopic.source_id == lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J✅ ==> 定级J
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I✅==> 定级I
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 11) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H✅ ==> 定级H
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I✅ ==> 定级I
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 12) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G✅ ==> 定级G
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H✅ ==> 定级H
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 13) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F✅ ==> 定级F
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G✅ ==> 定级G
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 14) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E✅ ==> 定级E
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F✅ ==> 定级F
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 15) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E✅ ==> 定级E
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 16) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            }
        } else {
            // B✅ ==> 抽C
            Topic *topic = question.topics.lastObject;
            [question.topics removeObject:topic];
            [self p_saveHistoryTopic:self.curTopic flag:flag];
            self.curTopic = topic;
            return;
        }
    } else {
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            if (historyCount == 1) {
                if (history.isTrue) {
                    // B✅ C❌ ==> 抽B
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    // B❌ A❌ ==> 抽aa  (不管对错,都会被定级.)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic = topic;
                    return;
                }
            } else if (historyCount == 2) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // B❌ A✅ A❌ ==> 抽aa (不管对错,都会被定级.)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic = topic;
                } else {
                    if (self.curTopic.source_id == lastTopic.source_id) {
                        // B✅ C✅ C❌ ==> 抽B
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // B✅ C❌ B❌ ==> 抽A
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel - 1)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 3) {
                if (self.normalLevel == self.curTopic.bigLevel) {
                    //self.curTopic.source_id == lastTopic.source_id - 1
                    // B✅ C✅ C❌ B❌ ==> 抽A
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel - 1)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                } else if (self.normalLevel < self.curTopic.bigLevel) {
                    // B✅ C✅ C✅ D❌  ==> 直接定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:lastTopic];
                } else {
                    //self.curTopic.source_id == lastTopic.source_id - 1
                    // B✅ C❌ B❌ A❌ ==> 抽aa (不管对错,都会被定级.)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic = topic;
                }
                return;
            } else if (historyCount == 4){
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // B✅ C✅ C❌ B❌ A❌ ==> 抽aa (不管对错,都会被定级.)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic = topic;
                    return;
                } else if (self.curTopic.bigLevel > self.normalLevel) {
                    // B✅ C✅ C✅ D✅ E❌ ==> 抽D
                    question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == (self.curTopic.source_id - 1)) {
                            topic = obj;
                            *stop = YES;
                        } else {
                            if (idx == (question.topics.count - 1)) {
                                topic = obj;
                                *stop = YES;
                            }
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 5) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F❌ ==> 抽E
                    question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == (lastTopic.source_id)) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // B✅ C✅ C❌ B❌ A✅ A❌ ==> 抽aa (不管对错,都会定级.)
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                        self.gradeTopic = topic;
                    } else {
                        // B✅ C✅ C✅ D✅ E❌ D❌ ==> 直接定级C
                        Topic *gradeTopic = question.topics.lastObject;
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:gradeTopic];
                    }
                    return;
                }
            } else if (historyCount == 6) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];
                    __block Topic *topic;
                    // B✅ C✅ C✅ D✅ E✅ F✅ G❌ ==> 抽F
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == (lastTopic.source_id)) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F❌ E❌ ==> 抽D
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                    return;
                }
            } else if (historyCount == 7) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ ==> 抽G
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                    return;
                } else {
                    Topic *curBigTopic = question.topics.lastObject;
                    if (self.curTopic.source_id == (curBigTopic.source_id + 1)) {
                        // B✅ C✅ C✅ D✅ E✅ F❌ E❌ D❌==> 定级C
                        Topic *gradeTopic = question.topics.lastObject;
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:gradeTopic];
                        return;
                    } else {
                        // B✅ C✅ C✅ D✅ E✅ F✅ G❌ F❌ ==> 抽E
                        [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                        return;
                    }
                }
            } else if (historyCount == 8) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I❌ ==> 抽H
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G❌ F❌ E❌ ==> 抽D
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ G❌ ==> 抽F
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                    return;
                }
            } else if (historyCount == 9) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ ==> 抽I
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                    return;
                } else {
                    Topic *curBigTopic = question.topics.lastObject;
                    // B✅ C✅ C✅ D✅ E✅ F✅ G❌ F❌ E❌ D❌ ==> 定级C
                    if (self.curTopic.source_id == (curBigTopic.source_id + 1)) {
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:curBigTopic];
                        return;
                    } else {
                        // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ G❌ F❌ ==> 抽E
                        [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                        return;
                    }
                }
            } else if (historyCount == 10) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ ==> 抽I
                [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                return;
            } else if (historyCount == 11) {
                Topic *curBigTopic = question.topics.lastObject;
                if (self.curTopic.source_id == (curBigTopic.source_id + 1)) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:curBigTopic];
                    return;
                }  else {
                    // TODO:解决崩溃..
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌==> 抽H
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                }
                return;
            } else if (historyCount == 12) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ ==> 抽G
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ ==> 抽F
                [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                return;
            } else if (historyCount == 13) {
                Topic *curBigTopic = question.topics.lastObject;
                if (self.curTopic.source_id == (curBigTopic.source_id + 1)) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ ==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:curBigTopic];
                    return;
                }  else {
                    // TODO:解决崩溃...
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ ==> 抽F
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ ==> 抽E
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                }
                return;
            } else if (historyCount == 14) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ ==> 抽E
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ ==> 抽D
                [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                return;
            } else if (historyCount == 15) {
                Topic *curBigTopic = question.topics.lastObject;
                if (self.curTopic.source_id == (curBigTopic.source_id + 1)) {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:curBigTopic];
                }  else {
                    // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ ==> 抽D
                    [self p_fetchHighLevelDescendingNextTopicWithFlag:flag];
                }
                return;
            } else if (historyCount == 16) {
                // B✅ C✅ C✅ D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ ==> 定级C
                Topic *gradeTopic = question.topics.lastObject;
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:gradeTopic];
                return;
            }
        } else {
            // B ❌ ==> 抽A
            [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
            return;
        }
    }
}

- (void)p_grade2WithLastTopic:(Topic *)lastTopic flag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.normalLevel)];
    if (flag) {
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            if (historyCount == 1) {
                if (history.isTrue) {
                    // D✅ E✅ ==> 抽F
                    [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // D❌ C✅ ==> 抽C
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 2) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // D✅ E✅ F✅ ==> 抽G
                    [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // D❌ C✅ C✅ ==> 定级C
                    // D✅ E❌ D✅ ==> 定级D
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 3) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ ==> 抽H
                    [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E❌ D❌ C✅ ==> 抽C
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // D✅ E✅ F❌ E✅ ==> 定级E
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                    }
                    return;
                }
            } else if (historyCount == 4) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ H✅ ==> 抽I
                    [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    // D✅ E✅ F✅ G❌ F✅ ==> 定级F
                    // D✅ E✅ F❌ E❌ D✅ ==> 定级D
                    // D✅ E❌ D❌ C✅ C✅ ==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                }
            } else if (historyCount == 5) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ ==> 抽J
                    [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
                    return;
                } else {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E✅ F❌ E❌ D❌ C✅ ==> 抽C
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // D✅ E✅ F✅ G✅ H❌ G✅ ==> 定级G
                        // D✅ E✅ F✅ G❌ F❌ E✅ ==> 定级E
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                    }
                    return;
                }
            } else if (historyCount == 6) {
                if (self.curTopic.source_id > lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ ==> 抽J
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    // D✅ E✅ F❌ E❌ D❌ C✅ C✅ ==> 定级C
                    // D✅ E✅ F✅ G✅ H✅ I❌ H✅ ==> 定级H
                    // D✅ E✅ F✅ G✅ H❌ G❌ F✅ ==> 定级F
                    // D✅ E✅ F✅ G❌ F❌ E❌ D✅ ==> 定级D
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
            } else if (historyCount == 7) {
                if (self.curTopic.source_id == lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J✅ ==> 定级J
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                    return;
                } else {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E✅ F✅ G❌ F❌ E❌ D❌ C✅ ==> 抽C
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.lastObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    } else {
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I✅ ==> 定级I
                        // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G✅ ==> 定级G
                        // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E✅ ==> 定级E
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        [self gradeCompleteWithTopic:self.curTopic];
                    }
                    return;
                }
            } else if (historyCount == 8) {
                // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I✅ ==> 定级I
                // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H✅ ==> 定级H
                // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F✅ ==> 定级F
                // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 9) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ C✅ ==> 抽C
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H✅ ==> 定级H
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G✅ ==> 定级G
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E✅ ==> 定级E
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
                return;
            } else if (historyCount == 10) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ C✅ C✅ ==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G✅ ==> 定级G
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F✅ ==> 定级F
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
                return;
            } else if (historyCount == 11) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ C✅ ==> 抽C
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F✅ ==> 定级F
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E✅ ==> 定级E
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
                return;
            } else if (historyCount == 12) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ C✅ C✅ ==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E✅ ==> 定级E
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
                return;
            } else if (historyCount == 13) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C✅ ==> 抽C
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D✅ ==> 定级D
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                }
                return;
            } else if (historyCount == 14) {
                if (self.curTopic.source_id == lastTopic.source_id) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C✅ C✅ ==> 定级C
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    [self gradeCompleteWithTopic:self.curTopic];
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C✅ ==> 抽C
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.lastObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                }
                return;
            } else if (historyCount == 15) {
                // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C✅ C✅ ==> 定级C
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                [self gradeCompleteWithTopic:self.curTopic];
                return;
            } else if (historyCount == 16) {
                
            }
        } else {
            // D✅ ==> 抽E
            [self p_fetchCurLevelAscendingNextTopicWithFlag:flag];
            return;
        }
    } else {
        if (lastTopic) {
            NSUInteger historyCount = self.history_topic.count;
            History *history = self.history_topic.lastObject;
            if (historyCount == 1) {
                if (history.isTrue) {
                    // D✅ E❌ ==> 抽D
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                } else {
                    // D❌ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                    return;
                }
            } else if (historyCount == 2) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D❌ C✅ C❌  ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    if (self.curTopic.source_id < lastTopic.source_id) {
                        // D✅ E❌ D❌ ==> 抽C
                        [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                    } else {
                        // D✅ E✅ F❌ ==> 抽E
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 3) {
                if (self.curTopic.source_id < lastTopic.source_id) {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                        self.gradeTopic= topic;
                    } else {
                        // D✅ E✅ F❌ E❌ ==> 抽D
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                    return;
                } else {
                    // D✅ E✅ F✅ G❌ ==> 抽F
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == self.curTopic.source_id - 1) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 4) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E❌ D❌ C✅ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    if (self.curTopic.source_id < lastTopic.source_id) {
                        Topic *curSmallTopic = question.topics.firstObject;
                        if (self.curTopic.source_id == curSmallTopic.source_id) {
                            // D✅ E✅ F❌ E❌ D❌ ==> 抽C
                            [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                        } else {
                            // D✅ E✅ F✅ G❌ F❌ ==> 抽E
                            __block Topic *topic;
                            [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (obj.source_id == self.curTopic.source_id - 1) {
                                    topic = obj;
                                    *stop = YES;
                                }
                            }];
                            [question.topics removeObject:topic];
                            [self p_saveHistoryTopic:self.curTopic flag:flag];
                            self.curTopic = topic;
                        }
                    }  else {
                        // D✅ E✅ F✅ G✅ H❌ ==> 抽G
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 5) {
                if (self.curTopic.source_id < lastTopic.source_id) {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E✅ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                        self.gradeTopic= topic;
                    } else {
                        // D✅ E✅ F✅ G✅ H❌ G❌ ==> 抽F
                        // D✅ E✅ F✅ G❌ F❌ E❌ ==> 抽D
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                    return;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I❌ ==> 抽H
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == self.curTopic.source_id - 1) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 6) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F❌ E❌ D❌ C✅ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    if (self.curTopic.source_id < lastTopic.source_id) {
                        Topic *curSmallTopic = question.topics.firstObject;
                        if (self.curTopic.source_id == curSmallTopic.source_id) {
                            // D✅ E✅ F✅ G❌ F❌ E❌ D❌ ==> 抽C
                            [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                        } else {
                            // D✅ E✅ F✅ G✅ H❌ G❌ F❌ ==> 抽E
                            // D✅ E✅ F✅ G✅ H✅ I❌ H❌ ==> 抽G
                            __block Topic *topic;
                            [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (obj.source_id == self.curTopic.source_id - 1) {
                                    topic = obj;
                                    *stop = YES;
                                }
                            }];
                            [question.topics removeObject:topic];
                            [self p_saveHistoryTopic:self.curTopic flag:flag];
                            self.curTopic = topic;
                        }
                        return;
                    } else {
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ ==> 抽I
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                        return;
                    }
                }
            } else if (historyCount == 7) {
                if (self.curTopic.source_id < lastTopic.source_id) {
                    if (self.curTopic.bigLevel < self.normalLevel) {
                        // D✅ E✅ F✅ G❌ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                        question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                        Topic *topic = question.topics.firstObject;
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                        self.gradeTopic= topic;
                    } else {
                        // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ ==> 抽D
                        // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ ==> 抽F
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ ==> 抽H
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                    return;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ ==> 抽I
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == self.curTopic.source_id - 1) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    return;
                }
            } else if (historyCount == 8) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G❌ F❌ E❌ D❌ C✅ C❌ ==> 抽B ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    Topic *curSmallTopic = question.topics.firstObject;
                    if (self.curTopic.source_id == curSmallTopic.source_id) {
                        // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ ==> 抽C
                        [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                    } else {
                        // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ ==> 抽E
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ ==> 抽G
                        // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ ==> 抽H
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 9) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ ==> 抽D
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ ==> 抽F
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ ==> 抽G
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == self.curTopic.source_id - 1) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                }
                return;
            } else if (historyCount == 10) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H❌ G❌ F❌ E❌ D❌ C✅ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    Topic *curSmallTopic = question.topics.firstObject;
                    if (self.curTopic.source_id == curSmallTopic.source_id) {
                        // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ ==> 抽C
                        [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                    } else {
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ ==> 抽E
                        // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ ==> 抽F
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 11) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ ==> 抽D
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ ==> 抽E
                    __block Topic *topic;
                    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if (obj.source_id == self.curTopic.source_id - 1) {
                            topic = obj;
                            *stop = YES;
                        }
                    }];
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                }
                return;
            } else if (historyCount == 12) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I❌ H❌ G❌ F❌ E❌ D❌ C✅ C❌ ==> 抽B (不管对错,都定级)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    Topic *curSmallTopic = question.topics.firstObject;
                    if (self.curTopic.source_id == curSmallTopic.source_id) {
                        // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ ==> 抽C
                        [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                    } else {
                        // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ ==> 抽D
                        __block Topic *topic;
                        [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (obj.source_id == self.curTopic.source_id - 1) {
                                topic = obj;
                                *stop = YES;
                            }
                        }];
                        [question.topics removeObject:topic];
                        [self p_saveHistoryTopic:self.curTopic flag:flag];
                        self.curTopic = topic;
                    }
                }
                return;
            } else if (historyCount == 13) {
                if (self.curTopic.bigLevel < self.normalLevel) {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                    question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                    Topic *topic = question.topics.firstObject;
                    [question.topics removeObject:topic];
                    [self p_saveHistoryTopic:self.curTopic flag:flag];
                    self.curTopic = topic;
                    self.gradeTopic= topic;
                } else {
                    // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ ==> 抽C
                    [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
                }
                return;
            } else if (historyCount == 14) {
                // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C❌ ==> 抽B (不管对错,定级B)
                question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                Topic *topic = question.topics.firstObject;
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                self.gradeTopic= topic;
            } else if (historyCount == 15) {
                // D✅ E✅ F✅ G✅ H✅ I✅ J✅ J❌ I❌ H❌ G❌ F❌ E❌ D❌ C✅ C❌ ==> 抽B (不管对错,定级B)
                question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
                Topic *topic = question.topics.firstObject;
                [question.topics removeObject:topic];
                [self p_saveHistoryTopic:self.curTopic flag:flag];
                self.curTopic = topic;
                self.gradeTopic= topic;
            }
        } else {
            // D❌ ==> 抽C
            [self p_fetchLowLevelDescendingNextTopicWithFlag:flag];
        }
    }
}

- (void)p_fetchHighLevelDescendingNextTopicWithFlag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];
    __block Topic *topic;
    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.source_id == (self.curTopic.source_id - 1)) {
            topic = obj;
            *stop = YES;
        }
    }];
    [question.topics removeObject:topic];
    [self p_saveHistoryTopic:self.curTopic flag:flag];
    self.curTopic = topic;
}

- (void)p_fetchHighLevelAscendingNextTopicWithFlag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.normalLevel + 1)];
    __block Topic *topic;
    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.source_id == (self.curTopic.source_id + 1)) {
            topic = obj;
            *stop = YES;
        } else {
            if (idx == (question.topics.count - 1)) {
                topic = obj;
                *stop = YES;
            }
        }
    }];
    [question.topics removeObject:topic];
    [self p_saveHistoryTopic:self.curTopic flag:flag];
    self.curTopic = topic;
}

- (void)p_fetchLowLevelDescendingNextTopicWithFlag:(BOOL)flag {
    Question *question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel - 1)];
    Topic *topic = question.topics.lastObject;
    [question.topics removeObject:topic];
    [self p_saveHistoryTopic:self.curTopic flag:flag];
    self.curTopic = topic;
}

- (void)p_fetchCurLevelAscendingNextTopicWithFlag:(BOOL)flag {
    __block Topic *topic;
    Question *question = [self.allTopicDic objectForKey:@(self.curTopic.bigLevel)];
    [question.topics enumerateObjectsUsingBlock:^(Topic *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.source_id == self.curTopic.source_id + 1) {
            topic = obj;
            *stop = YES;
        }
    }];
    [question.topics removeObject:topic];
    [self p_saveHistoryTopic:self.curTopic flag:flag];
    self.curTopic = topic;
}

@end
